﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class UIElementHandle
{
    //保存所有的ui元素
    private Dictionary<string, BaseUIElement> uiElementDic;
    //ui类型
    private string[] typeNames = new string[] { "Button" , "Text"};
    private BasePanel panel;

    public UIElementHandle(ref List<GameObject> uiElementObjList, BasePanel panel) {
        panel.panelName = panel.gameObject.name;
        this.panel = panel;
        init(ref uiElementObjList, panel);

    }

    //查找按钮类型的ui元素
    public ButtonUIElement FindButton(string name) {
        return uiElementDic.GetValue(name + "Button") as ButtonUIElement;
    }
    //查找Text类型的ui元素
    public TextUIElement FindText(string name)
    {
        return uiElementDic.GetValue(name + "Text") as TextUIElement;
    }

    //查找所有类型的ui元素
    public BaseUIElement FindUIElement(string name, string type)
    {
        return uiElementDic.GetValue(name + type);
    }

    //对ui元素进行初始化处理：绑定方法、处理命令、并保存到字典中去
    protected void init(ref List<GameObject> uiElementObjList, BasePanel panel)
    {
        uiElementDic = new Dictionary<string, BaseUIElement>();
        for (int i = 0; i < uiElementObjList.Count; i++)
        {
            GameObject temp = uiElementObjList[i];
            foreach (string typeName in typeNames)
            {
                if (IsExistType(temp, typeName))
                {
                    UIOrderParse uiOrder = (UIOrderParse)Activator.CreateInstance(Type.GetType(typeName + "OrderParse"), new object[] { panel });
                    string[] parseName = ParseGameObjectName(temp.name);
                    string objName = parseName[0];
                    string actionOrder = parseName.Length>1? parseName[1] : "";
                    BaseUIElement tempUI = AddComponent(temp, typeName);
                    //绑定点击方法
                    tempUI.SetMethodAndName(panel, objName);
                    //处理命令
                    uiOrder.Parse(actionOrder, tempUI);
                    //添加到字典
                    AddToDictionary(objName, typeName, tempUI);
                    break;
                }
            }
        }
    }

    private string[] ParseGameObjectName(string name) {
        return name.Split('>');
    }

    //为不同的类型字符串增加不同的组件
    private BaseUIElement AddComponent(GameObject obj, string typeName)
    {
        if (obj == null) {
            Debug.LogWarning(panel.panelName + " 中的UIElementList中存在空元素");
            return null;
        }
        if (typeName == "Button") {
            return obj.AddComponent<ButtonUIElement>();
        }

        if (typeName == "Text")
        {
            return obj.AddComponent<TextUIElement>();
        }
        return null;
    }

    //判断是否存在组件
    private bool IsExistType(GameObject obj, string name) {
        if (obj == null) {
            Debug.LogWarning(panel.panelName + " 中的UIElementList中存在空元素");
            return false;
        }
        if (name == "Button") {
            return obj.GetComponent<Button>() != null;
        }

        if (name == "Text")
        {
            return obj.GetComponent<Text>() != null;
        }

        return false;
    }

    private void AddToDictionary(string objName, string typeName, BaseUIElement tempUI) {
        try
        {
            if (uiElementDic.ContainsKey(objName + typeName))
            {
                uiElementDic.Add(objName + "_r" + typeName, tempUI);
            }
            else {
                uiElementDic.Add(objName + typeName, tempUI);
            }

        }
        catch {
            Debug.LogError("你往 "+panel.panelName+" 中的UIElementList中添加了名称重复的UI元素");
        }
    }
}
